package servlet.upload;

import dao.upload.AccessoryDao;
import entity.upload.EntityAccessory;
import util.FileUtils;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * 删除已上传文件
 *
 * @author xusucheng
 * @create 2017-12-30
 **/
@WebServlet("/removeUploadedFile")
public class RemoveUploadedFileServlet extends HttpServlet {
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //String filePath = request.getParameter("filePath");
        int fileId = Integer.valueOf(request.getParameter("id"));
        EntityAccessory entity = AccessoryDao.load(fileId);
        //删除文件
        FileUtils.delete(entity.getFilePath());
        //删除数据库记录
        AccessoryDao.remove(fileId);

        //跳回到文件列表页
        //request.getRequestDispatcher("listUploadedFiles").forward(request, response);
        response.sendRedirect("listUploadedFiles");
    }

    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doPost(request, response);
    }


}
